/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.mts.client.core.controller;

import com.google.common.collect.Lists;
import com.intelligent.mts.client.utils.ApplicationBeanUtils;
import com.intelligent.mts.client.utils.SystemConfig;
import com.intelligent.mts.common.utils.StringUtils;
import com.intelligent.mts.core.dto.*;
import com.intelligent.mts.core.service.DataSearchService;
import com.intelligent.mts.core.utils.AppConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by cherry on 2016/7/25.
 */
@Component
public class MainController {

    private final Logger logger = LoggerFactory.getLogger(MainController.class);

    @Autowired
    private DataSearchService dataSearchService;

    public ResultDto findAllData (String snValue) {
        ResultDto resultDto = new ResultDto();
//        try {
            resultDto = dataSearchService.searchData(snValue);
//        } catch (ApplicationException ex) {
           // ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());

//        }
        return resultDto;
    }

    public void updateCurrentStatusToOK (String productionId, String Status) {
//        try {
            dataSearchService.updateCurrentStatusToOK(productionId, Status);
//        } catch (ApplicationException ex) {
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//        }
    }

    public List<CustomerDto> findAllCustomer(){

        List<CustomerDto> list = new ArrayList<CustomerDto>();
//        try {
            list = dataSearchService.findCustomers();
//        }catch (ApplicationException ex){
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//        }
        return list;
    }

    public List<String> findAllFGPN(String customerName){

        List<String> list = new ArrayList<String>();
//        try {
            list = dataSearchService.findFgPnsByCustomer(customerName);
//        }catch (ApplicationException ex){
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//
//        }
        return list;
    }

    public List<String>findAllFGPN(){
        List<CustomerDto> Customerlist = new ArrayList<CustomerDto>();
        List<String> FgpnList = new ArrayList<>();
        Customerlist = dataSearchService.findCustomers();
        List<String> list = new ArrayList<>();
        if (Customerlist == null || Customerlist.isEmpty()){
            return null;
        }
        for (CustomerDto customer : Customerlist){
            list = dataSearchService.findFgPnsByCustomer(customer.getName());
            if (list != null && !list.isEmpty()){
                FgpnList.addAll(list);
            }
        }
        return FgpnList;
    }

    public List<FinishGoodDto> findFinishGoods(String customerName, String fgPn){

        List<FinishGoodDto> list = new ArrayList<FinishGoodDto>();
//        try {
            list = dataSearchService.findFinishGoodsByCustomerAndFgPn(customerName, fgPn);
//        }catch (ApplicationException ex){
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//        }

        return list;

    }

    public void updateFinishGoodStatus(String status,String customer,String fgPn){
//        try {
            dataSearchService.updateFinishGoodStatus(status,customer, fgPn);
//        }catch (ApplicationException ex){
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//            return false;
//        }
    }

    public boolean validateCustomer(String name){
//        try {
            return dataSearchService.validateCustomer(name);
//        }catch (ApplicationException ex){
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//            return false;
//        }
    }

    public void createCustomer(CustomerDto customerDto){
//        try {
            dataSearchService.createCustomer(customerDto);
//        }catch (ApplicationException ex){
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//            return false;
//        }
    }

    public boolean validateFGPN(String fgPn){
//        try {
            return dataSearchService.validateFgPn(fgPn);
//        }catch (ApplicationException ex){
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//            return false;
//        }
    }

    public void createFinishGood(String customername, String fgpn) {
//        try {
            dataSearchService.createFinishGood(customername, fgpn);
//        } catch (ApplicationException ex) {
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//            return false;
//        }
    }

    public void updateFinishGood(Map<String, String> map, String customer, String fgPn){
//        try {
            dataSearchService.updateFinishGood(map, customer, fgPn);
//        } catch (ApplicationException ex) {
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//        }
    }

    public boolean validateGwAndPcba(String pn, String gw, String pcba){
//        try {
            return dataSearchService.validateFgPnByGwAndPcba(pn, gw, pcba);
//        } catch (ApplicationException ex) {
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//            return false;
//        }
    }

    public boolean validatePnIsInFgPn(List<String> pns){
//        try {
            return dataSearchService.validatePn(pns);
//        } catch (ApplicationException ex) {
//            ApplicationBeanUtils.getBean(MainStatusPanel.class).setStatusValue(ex.getMessage());
//            return false;
//        }
    }
    public void createTestTrack(String stationId){
        TrackDto trackDto = new TrackDto();
        String productId = switchProductIdByStationId(stationId);
        trackDto.setStationId(stationId);
        trackDto.setNextStation(stationId);
        trackDto.setLineId("OBC");
        trackDto.setStatus(AppConst.TestStatus);
        trackDto.setHou(AppConst.RESULT_OK);
        trackDto.setHs(AppConst.RESULT_OK);
        trackDto.setPcba(AppConst.RESULT_OK);
        trackDto.setProductionId(productId);
        if(dataSearchService.findTrackByProductionId(productId) != null){
            return;
        }
        dataSearchService.createTrack(trackDto);
    }

    public void createTestSnTable(String stationId,String snValue){
        SerialNumberDto serialNumberDto =  new SerialNumberDto();
        String serialName = "";
        String productId =switchProductIdByStationId(stationId);
        if(stationId.equals("0750")||stationId.equals("0650")||stationId.equals("0690")){
            serialName = "HS";
        }else {
            serialName ="HOU";
        }
        serialNumberDto.setProductionId(productId);
        serialNumberDto.setStationId(stationId);
        serialNumberDto.setSerialValue(snValue);
        serialNumberDto.setSerialName(serialName);
        serialNumberDto.setPnValue(null);
        List<SerialNumberDto> serialNumberDtoList = null;
        serialNumberDtoList = dataSearchService.findSnByStationId(stationId,productId);
        if(serialNumberDtoList.isEmpty()){
        dataSearchService.createSerialNumber(serialNumberDto);
        }else{
            dataSearchService.updateSerialNumber(serialNumberDto);
        }
    }

    public void createProduction(String stationId,String fgpn){
        ProductionDto productionDto = new ProductionDto();
        String productId = switchProductIdByStationId(stationId);
        productionDto.setProductionId(productId);
        productionDto.setFgPn(fgpn);
        productionDto.setParentId(null);
        if(dataSearchService.findProductionByProductionId(productId) != null){
            dataSearchService.updateProductionFgPn(productId,fgpn);
        }else {
            dataSearchService.createProduction(productionDto);
        }

    }

    public boolean validateSn(String sn){
        List<SerialNumberDto> serialNumberDtoList = null;
        serialNumberDtoList= dataSearchService.findSnBySerialValue(sn);
        if(serialNumberDtoList == null || serialNumberDtoList.isEmpty() ){
            return false;
        }
        return true;
    }
    public List<String> findAllTestStationId(){
        SystemConfig systemConfig = ApplicationBeanUtils.getBean(SystemConfig.class);
        if (StringUtils.isBlank(systemConfig.getTestStations())) {
            logger.info("stationId is empty.");
            return null;
        }
        List<String> StationIdLists = Lists.newArrayList();
        String[] stationIds = systemConfig.getTestStations().split(",");
        for (String stationId : stationIds) {
            StationIdLists.add(stationId);
        }
        return StationIdLists;
    }

    public String switchProductIdByStationId(String stationId){
        String productId = "";
        switch (stationId){
            case "0650":
                productId = AppConst.PRODUCTION_ID_0650;
                break;
            case "0690":
                productId = AppConst.PRODUCTION_ID_0690;
                break;
            case "0750":
                productId = AppConst.PRODUCTION_ID_0750;
                break;
            case "0990":
                productId = AppConst.PRODUCTION_ID_0990;
                break;
            case "1020":
                productId = AppConst.PRODUCTION_ID_1020;
                break;
            case "1030":
                productId = AppConst.PRODUCTION_ID_1030;
                break;
            case "1050":
                productId = AppConst.PRODUCTION_ID_1050;
                break;
            case "1060":
                productId = AppConst.PRODUCTION_ID_1060;
                break;
        }
        return productId;
    }

    public String findSerialNumberByStationId(String stationId){
        if (StringUtils.isBlank(stationId)){
            return null;
        }
        List<SerialNumberDto> serialNumberDtoList = null;
        String productId =switchProductIdByStationId(stationId);
        serialNumberDtoList = dataSearchService.findSnByStationId(stationId,productId);
        if(serialNumberDtoList.isEmpty()){
            return null;
        }else{
            return serialNumberDtoList.get(0).getSerialValue();
        }
    }

    public String findFgpnByStationId(String stationId){
        if (StringUtils.isBlank(stationId)){
            return null;
        }
        String productId = switchProductIdByStationId(stationId);
        if(dataSearchService.findProductionByProductionId(productId) == null){
            return null;
        }else {
            return dataSearchService.findProductionByProductionId(productId).getFgPn();
        }
    }
}
